package br.com.financeiro;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import br.com.financeiro.dao.ContaDao;
import br.com.financeiro.model.Conta;

public class ContaActivity extends Activity {
	
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
	private ListView listView;
	private List<Conta> listaContas;
	Button cadastrarConta;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_conta);

		listView = (ListView) findViewById(R.id.ListagemContas);

		exibirLista();
		
		cadastrarConta = (Button) findViewById(R.id.cadastrarContaConta);
		cadastrarConta.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent cadastrarContaIntent = new Intent(ContaActivity.this,
						CadastrarContaActivity.class);
				startActivity(cadastrarContaIntent);
			}
		});
	}

	public void exibirLista() {
		try {
			String[] from = new String[] { "descricao", "data", "valor", "tipo" };
			int[] to = new int[] { R.id.listDescricao, R.id.listData,
					R.id.listValor, R.id.listPago };

			ContaDao contaDao = ContaDao.getInstance(getApplicationContext());
			listaContas = contaDao.recuperarTodos();
			Log.i("LISTA 2", listaContas.size() + "");

			List<HashMap<String, String>> mapItensLista = new ArrayList<HashMap<String, String>>();
			for (int i = 0; i < listaContas.size(); i++) {
				HashMap<String, String> mapItens = new HashMap<String, String>();
				String data =null;
				try {
					data = formato.format(listaContas.get(i).getData());	
				} catch (Exception e) {
					// TODO: handle exception
				}
				mapItens.put("descricao", listaContas.get(i).getDescricao());
				mapItens.put("data",data );
				mapItens.put("valor", String.valueOf(listaContas.get(i).getValor()));
				mapItens.put("tipo", listaContas.get(i).getTipo().getDescricao());

				mapItensLista.add(mapItens);
			}

			SimpleAdapter adapter = new SimpleAdapter(this, mapItensLista,
					R.layout.item_listview, from, to);
			listView.setAdapter(adapter);

			listView.setOnItemClickListener(new OnItemClickListener() {

		            @Override
		            public void onItemClick(AdapterView<?> arg0, View arg1,
		                    int position, long arg3) {
		            	//enviar posicao do tipo de conta que foi selecionado para a activity editarExcluir
		               Intent editarContaIntent = new Intent(ContaActivity.this,EditarContaActivity.class);
		               editarContaIntent.putExtra("posicao", position);
		               startActivity(editarContaIntent);
		            }
		        });
			
			//Log.i("VALOR A PAGAR", banco.getValorAPagar() + "");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		exibirLista();
	}

	private void showCustomDialog(final Conta conta) {
		final Dialog dialog = new Dialog(ContaActivity.this);
		dialog.setContentView(R.layout.custom_dialog);
		dialog.setTitle("Informações da consulta");

		final Button btOk = (Button) dialog.findViewById(R.id.btOk);
		final RadioGroup groupAcao = (RadioGroup) dialog.findViewById(R.id.groupAcao);

		btOk.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				switch (groupAcao.getCheckedRadioButtonId()) {
					case R.id.radioExcluir:
						//banco.excluir(conta);
						
						finish();
						startActivity(getIntent());
						
						Toast.makeText(ContaActivity.this, "Excluiu",
								Toast.LENGTH_SHORT).show();
						dialog.dismiss();
	
						break;
					case R.id.radioPago:
						Toast.makeText(ContaActivity.this, "Pagou",
								Toast.LENGTH_SHORT).show();
						dialog.dismiss();
						
						break;
	
					case R.id.radioNaoPago:
						Toast.makeText(ContaActivity.this, "N�o pagou",
								Toast.LENGTH_SHORT).show();
						dialog.dismiss();
						
						break;
				}
			}
		});

		dialog.show();
	}
}
